﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using ServEvo.Layout.Interfaces;
using ServEvo.Layout.Enum;

namespace ServEvo.ServControl
{
    class ServeurManager : MainManager, IServeur, IDisposable
    {
        protected List<Service> _listServices;
        protected Dictionary<Service, Process> _listProcess;
        protected Dictionary<Service, ProcessStartInfo> _listProcessStartInfo;
        protected Dictionary<Service, int> _listProcessId;

        public void Dispose()
        {
            stopServeur();
            foreach (KeyValuePair<Service, Process> o in _listProcess)
            {
                o.Value.Dispose();
            }
        }

        List<Service> getListServeurs()
        {
            return _listServices;
        }
        List<Service> IServeur.getListServeurs()
        {
            return _listServices;
        }

        protected void initList()
        {
            _listProcess = new Dictionary<Service, Process>();
            _listProcessStartInfo = new Dictionary<Service, ProcessStartInfo>();
            _listProcessId = new Dictionary<Service, int>();

            foreach (Service service in _listServices)
            {
                _listProcess.Add(service, null);
                _listProcessStartInfo.Add(service, null);
                _listProcessId.Add(service, 0);
            }
        }

        public bool isServeurStarted()
        {
            bool bStatut = true;
            foreach (Service serveur in _listServices)
            {
                bStatut &= isServeurStarted(serveur);
            }
            return bStatut;
        }
        public bool isServeurStarted(Service serveur)
        {
            bool bStatut = false;
            if (_listServices.Contains(serveur))
            {
                if (_listProcessId.ContainsKey(serveur) && _listProcessId[serveur] != 0)
                {
                    bStatut = true;
                }
            }
            return bStatut;
        }

        public void startServeur()
        {
            foreach (Service serveur in _listServices)
            {
                startServeur(serveur);
            }
        }
        public void startServeur(Service serveur)
        {
            lock (oLock)
            {
                if (_listServices.Contains(serveur))
                {
                    try
                    {
                        _listProcess[serveur] = Process.Start(_listProcessStartInfo[serveur]);
                        _listProcessId[serveur] = _listProcess[serveur].Id;
                    }
                    catch (Exception)
                    {
                        _listProcessId[serveur] = 0;
                    }
                }
            }
        }

        public void restartServeur()
        {
            foreach (Service serveur in _listServices)
            {
                restartServeur(serveur);
            }
        }
        public void restartServeur(Service serveur)
        {
            lock (oLock)
            {
                if (_listServices.Contains(serveur))
                {
                    startServeur(serveur);
                    stopServeur(serveur);
                }
            }
        }

        public void stopServeur()
        {
            foreach (Service serveur in _listServices)
            {
                stopServeur(serveur);
            }
        }
        public void stopServeur(Service serveur)
        {
            lock (oLock)
            {
                if (_listServices.Contains(serveur))
                {
                    try
                    {
                        if (_listProcessId.ContainsKey(serveur) && _listProcessId[serveur] > 0)
                        {
                            _listProcess[serveur] = Process.GetProcessById(_listProcessId[serveur]);
                            Process[] pListHTTP = Process.GetProcessesByName(_listProcess[serveur].ProcessName);
                            foreach (Process pHttp in pListHTTP)
                            {
                                pHttp.Kill();
                            }
                            _listProcessId[serveur] = 0;
                        }
                    }
                    catch (Exception) { _listProcessId[serveur] = 0; }
                }
            }
        }
    }
}
